package Atm01;

import Tz1.Strin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 改密界面
 */
public class ChangePwdPanel extends JPanel implements ActionListener {
   private ATMFrame atmFrame;
    private JLabel passwordJLable=new JLabel("原密码:");
    private JPasswordField pwdField = new JPasswordField();

    private JLabel passwordJLable1=new JLabel("新密码:");
    private JPasswordField pwdField1 = new JPasswordField();

    private JLabel passwordJLable2=new JLabel("再次输入新密码:");
    private JPasswordField pwdField2 = new JPasswordField();


    private ImageIcon sureImg= new ImageIcon("Images/buttonSure.jpg");
    private ImageIcon clearImg= new ImageIcon("Images/buttonClear.jpg");

    private Font font = new Font("黑体",Font.BOLD,30);

    private JButton sureBth= new JButton(sureImg);
    private JButton clearBth= new JButton(clearImg);




    public ChangePwdPanel(ATMFrame atmFrame){
        this.setLayout(null);
        this.atmFrame=atmFrame;
        sureBth.setBounds(530,480,140,50);
        clearBth.setBounds(360,480,150,50);

         passwordJLable.setBounds(260,200,120,50);
         passwordJLable.setFont(font);

         pwdField.setBounds(400,200,250,50);
         pwdField.setFont(font);


         passwordJLable1.setBounds(260,300,120,50);
         passwordJLable1.setFont(font);

         pwdField1.setBounds(400,300,250,50);
         pwdField1.setFont(font);


        passwordJLable2.setBounds(160,400,250,50);
        passwordJLable2.setFont(font);

        pwdField2.setBounds(400,400,250,50);
        pwdField2.setFont(font);


        this.add(passwordJLable);
        this.add(pwdField);
        this.add(pwdField1);
        this.add(pwdField2);
        this.add(passwordJLable1);
        this.add(passwordJLable2);
        this.add(sureBth);
        this.add(clearBth);


        clearBth.addActionListener(this);
        sureBth.addActionListener(this);
        this.setOpaque(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sureBth) {
            User user = atmFrame.getLoginUser();
             String user1=(String.valueOf(pwdField.getPassword()));
            if (user1.equals(user.getPwd())) {
                String user2=(String.valueOf(pwdField1.getPassword()));
                String user3=(String.valueOf(pwdField2.getPassword()));
                if (user3.equals(user2)) {
                    user.setPwd(String.valueOf(pwdField1.getPassword()));
                    JOptionPane.showMessageDialog(this, "修改成功,退出登录");
                    atmFrame.switchPanel("login");
                } else {
                    JOptionPane.showMessageDialog(this, "抱歉,两次密码不一致");
                }
            } else {
                JOptionPane.showMessageDialog(this, "输入原密码错误");
            }
        }
        if (e.getSource() == clearBth) {
            atmFrame.switchPanel("menu");

        }
    }
}